package com.effective.java.equals;

import java.awt.Color;

public class BrokenSymmetry02 {

	public static void main(String[] args) {

		Point p1 = new Point(2, 1);
		Point p2 = new ColorPoint(2, 1, Color.BLUE);
		System.out.println("p1.equals(p2) = " + p1.equals(p2));
		System.out.println("p2.equals(p1) = " + p2.equals(p1));
	}

	private static class Point {

		private final int x;
		private final int y;

		public Point(int x, int y) {

			this.x = x;
			this.y = y;
		}

		/**
		 * {@inheritDoc}
		 */
		@Override
		public boolean equals(Object object) {

			if (!(object instanceof Point)) {
				return false;
			}

			Point p = (Point) object;
			return (p.x == this.x && p.y == this.y);
		}

	}

	private static class ColorPoint extends Point {

		private final Color color;

		public ColorPoint(int x, int y, Color color) {

			super(x, y);
			this.color = color;
		}

		/**
		 * {@inheritDoc}
		 */
		@Override
		public boolean equals(Object object) {

			/*
			 * This breaks the symmetry requirement because now
			 * ColorPoint.equals(Point) will probably result differently from
			 * Point.equals(ColorPoint)
			 */
			if (!(object instanceof ColorPoint)) {
				return false;
			}

			return (super.equals(object) && ((ColorPoint) object).color.equals(this.color));
		}
	}
}
